package es.aradesh.ministryassist.logic.ministry;

import java.util.Iterator;
import java.util.List;

import android.content.ContentValues;

import es.aradesh.ministryassist.db.DbAdapter;
import es.aradesh.ministryassist.db.StreetColumns;

public class Street implements MinistryElement{
	public final static int TYPE_CONSECUTIVOS = 0;
	public final static int TYPE_PARES = 1;
	public final static int TYPE_IMPARES = 2;
	public final static int TYPE_AMBOS = 3;
	
	private long id;
	private Territory territory;
	private String name;
	private int type;
	
	/**
	 * @param id
	 * @param territory
	 * @param name
	 * @param type
	 */
	public Street(long id, Territory territory, String name, int type) {
		super();
		this.id = id;
		this.territory = territory;
		this.name = name;
		this.type = type;
	}
	/**
	 * @return the id
	 */
	public long getId() {
		return id;
	}
	/**
	 * @return the territoryId
	 */
	public Territory getTerritory() {
		return territory;
	}
	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}
	/**
	 * @return the type
	 */
	public int getType() {
		return type;
	}
	/**
	 * @param id the id to set
	 */
	public void setId(long id) {
		this.id = id;
	}
	/**
	 * @param territoryId the territory to set
	 */
	public void setTerritory(Territory territory) {
		this.territory = territory;
	}
	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}
	/**
	 * @param type the type to set
	 */
	public void setType(int type) {
		this.type = type;
	}
	public boolean delete(DbAdapter mDb){
		List<Home> ls = HomeBuilder.getHomesByStreet(mDb, this);
		Iterator<Home> lsi = ls.iterator();
		while(lsi.hasNext()){
			lsi.next().delete(mDb);
		}
		return mDb.removeById(DbAdapter.TABLE_STREET, this.id);
	}
	
	public void refreshDB(DbAdapter mDb){
		ContentValues cv = new ContentValues(3);
		cv.put(StreetColumns.NAME, this.name);
		cv.put(StreetColumns.TERRITORY_ID, this.territory.getId());
		cv.put(StreetColumns.TYPE, this.type);
		mDb.updateById(DbAdapter.TABLE_STREET, this.id, cv);	
	}
	public String toString(){
		return this.name;
	}
	/* (non-Javadoc)
	 * @see es.aradesh.ministryassist.logic.ministry.MinistryElement#getActivityEditType()
	 */
	public int getActivityEditType() {
		return MinistryElement.ACTIVITY_EDIT_STREET;
	}
	/* (non-Javadoc)
	 * @see es.aradesh.ministryassist.logic.ministry.MinistryElement#getParam(java.lang.String)
	 */
	public Object getParam(String parameter) {
		Object result = null;
		if(parameter == StreetColumns.NAME) result = this.name;
		if(parameter == StreetColumns.TYPE) result = this.type;
		return result;
	}
}
